package com.example.myboot.controller;



import com.example.myboot.entity.Article;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;


@Controller
public class ArticleController {
    private static File textDir;
    static{
            textDir = new File("./articles");
            if (!textDir.exists()){
                textDir.mkdirs();
            }
    }

     @RequestMapping("/myweb/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response){


        String username = request.getParameter("username");
        String textname = request.getParameter("textname");
        String text = request.getParameter("text");

        if (username==null||textname==null){
//            File file = new File(staticDir,"/myweb/article_fail.html");
//            response.setContentFile(file);
            try {
                response.sendRedirect("/myweb/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        File file = new File(textDir,textname+".obj");
        if (file.exists()){
//            File file1 = new File(staticDir,"/myweb/article_fail.html");
//            response.setContentFile(file1);
            try {
                response.sendRedirect("/myweb/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

//        File file1 = new File(staticDir,"/myweb/article_success.html");
//        response.setContentFile(file1);
         try {
             response.sendRedirect("/myweb/article_success.html");
         } catch (IOException e) {
             e.printStackTrace();
         }

         //保存文章
        File textFile = new File(textDir,textname+".obj");
        Article article = new Article(username,textname,text);
        try {
            FileOutputStream fos = new FileOutputStream(textFile);
            ObjectOutputStream ois = new ObjectOutputStream(fos);
            ois.writeObject(article);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping("/myweb/showAllArticle")
    public void showAllArticle(HttpServletRequest request,HttpServletResponse response){

        //把文章添加到列表中
        List<Article> list = new ArrayList<>();


        File[] files = textDir.listFiles(f->f.getName().endsWith(".obj"));

        for (File f : files) {
            try(FileInputStream fis = new FileInputStream(f);
                ObjectInputStream ois = new ObjectInputStream(fis);
            ) {
                list.add((Article) ois.readObject());

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }

        //拼接HTML代码
        PrintWriter pr = null;
        //设置响应头
        response.setContentType("text/html;charset=utf-8");
        try {
            pr = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        pr.println("<!DOCTYPE html>");
        pr.println("<html lang=\"en\">");
        pr.println("<head>");
        pr.println("<meta charset=\"UTF-8\">");
        pr.println("<body>");
        pr.println("<center>");
        pr.println("<h1>文章列表</h1>");
        pr.println("  <table border=\"1\">");
        pr.println(" <tr>");
        pr.println(" <td>文章名</td>");
        pr.println("<td>作者名</td>");
        pr.println("</tr>");
        for (Article a : list){
            pr.println("<tr>");
            pr.println("<td>"+a.getTextname()+"</td>");
            pr.println("<td>"+a.getUsername()+"</td>");
            pr.println("</tr>");
        }

        pr.println("</table>");
        pr.println("</center>");
        pr.println("</body>");
        pr.println("</html>");



    }

}
